package com.sky.controller.admin;

import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

//菜品管理
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /*
     * @Description  新增菜品
     * @Param [dishDTO]
     * @return com.sky.result.Result
     **/
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
       dishService.saveWithFlavor(dishDTO);
//       清理缓存数据
        String key="dish_"+dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /*
     * @Description  分页查询
     * @Param [pageQueryDTO]
     * @return com.sky.result.Result<com.sky.result.PageResult>
     **/
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO pageQueryDTO){

       PageResult pageResult=dishService.pageQuery(pageQueryDTO);
       return Result.success(pageResult);
    }

    /*
     * @Description  批量删除
     * @Param [ids]
     * @return com.sky.result.Result
     **/
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除:{}",ids);
        dishService.deleteBatch(ids);
//        删除缓存
        cleanCache("dish_*");
        return Result.success();
    }

    /*
     * @Description  数据回显
     * @Param [id]
     * @return com.sky.result.Result<com.sky.vo.DishVO>
     **/
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品:{}",id);
       DishVO dishVO=dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /*
     * @Description  修改菜品
     * @Param [dishDTO]
     * @return com.sky.result.Result
     **/
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.updateWithFlavor(dishDTO);
//        删除缓存
        cleanCache("dish_*");
        return Result.success();
    }

    /*
     * @Description  修改菜品状态
     * @Param [status, id]
     * @return com.sky.result.Result
     **/
    @PostMapping("/status/{status}")
    public Result StartOrStop(@PathVariable Integer status,Long id){
        Dish dish=new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishService.updateStatus(dish);
//        删除缓存
        cleanCache("dish_*");
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<Dish>> list(Dish dish){
       List<Dish> list=dishService.list(dish);
       return Result.success(list);
    }

    /*
     * @Description  删除缓存的统一方法
     * @Param [pattern]
     * @return void
     **/
   public void cleanCache(String pattern){
       Set keys = redisTemplate.keys(pattern);
       redisTemplate.delete(keys);
   }
}
